package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.BrowseRecord;
import cn.com.connext.scrm.repository.master.BrowseRecordRepository;
import cn.com.connext.scrm.service.BrowseRecordService;
import cn.com.connext.scrm.service.dto.BrowseRecordDTO;
import cn.com.connext.scrm.service.mapper.BrowseRecordActivityService;
import cn.com.connext.scrm.service.mapper.BrowseRecordMapper;
import cn.com.connext.scrm.web.rest.req.BrowseRecordReq;
import cn.com.connext.scrm.web.rest.resp.BrowseRecordResp;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.exception.ErrorCode;
import com.connext.common.exception.ServiceException;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URISyntaxException;
import java.time.ZonedDateTime;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/api/browserecord")
public class BrowseRecordResource extends BaseResource {

    private final Logger log = LoggerFactory.getLogger(BrowseRecordResource.class);

    @Autowired
    private BrowseRecordMapper browseRecordMapper;

    @Autowired
    private BrowseRecordService browseRecordService;

    @Autowired
    private BrowseRecordActivityService BrowseRecordActivityService;

    public BrowseRecordResource() throws TimeoutException {
        super("BrowseRecord");
    }

    @PostMapping("/create")
    @Timed
    @ApiOperation(value = "添加一条浏览记录", tags = {"浏览记录列表"}, notes = "添加一条浏览记录")
    public ResponseEntity<BrowseRecordDTO> create(@RequestBody BrowseRecordDTO browseRecordDTO) throws URISyntaxException {
        if (StringUtils.isNotEmpty(browseRecordDTO.getId())) {
            throw new ServiceException(ErrorCode.ERROR_ID_EXISTS);
        }
        browseRecordDTO.setCreateTime(ZonedDateTime.now());
        BrowseRecord browseRecord = browseRecordMapper.toEntity(browseRecordDTO);
        BrowseRecordDTO result = browseRecordService.saveBrowseRecord(browseRecord);
        return createSuccess(result);
    }

    /**
     * 活动记录表
     */
    @PostMapping("/create-activity")
    @Timed
    @ApiOperation(value = "添加一条浏览记录", tags = {"浏览记录列表"}, notes = "添加一条浏览记录")
    public ResponseEntity createActivity(@RequestBody BrowseRecordReq req) throws URISyntaxException {
        if (StringUtils.isNotEmpty(req.getId())) {
            throw new ServiceException(ErrorCode.ERROR_ID_EXISTS);
        }
        BrowseRecordResp browseRecordResp =BrowseRecordActivityService.saveBrowseRecord(req);
        return createSuccess(browseRecordResp);
    }
}
